<?php
/*************************************************************************************
   Copyright notice
   
   (c) 2002-2007 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
  
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license 
   from the author is found in LICENSE.txt distributed with these scripts.
  
   This script is distributed in the hope that it will be useful, but WITHOUT ANY 
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
*************************************************************************************

   JW Image Rotator Module v3 by breitsch (mail@casa-loca.com)

*************************************************************************************/

// ----------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day.");
}
// ----------------------------------------------------------------
		 
//Imagerotator module handle content part POST values

$content['imagerotator'] = array(

		'fmp_width'					=> intval($_POST['fmp_width']),
		'fmp_height'				=> intval($_POST['fmp_height']),
		'fmp_sort'					=> empty($_POST['fmp_sort']) ? 0 : intval($_POST['fmp_sort']),
		'fields'              => array(),
		'fmp_set_logo'				=> clean_slweg($_POST['fmp_set_logo']),
		'fmp_set_hcolor'			=> substr(preg_replace('/[^0-9a-f]/i', '', $_POST['fmp_set_hcolor']), 0, 6),
		'fmp_set_color'				=> substr(preg_replace('/[^0-9a-f]/i', '', $_POST['fmp_set_color']), 0, 6),
		'fmp_set_bgcolor'			=> substr(preg_replace('/[^0-9a-f]/i', '', $_POST['fmp_set_bgcolor']), 0, 6),
		'fmp_set_screencolor'			=> substr(preg_replace('/[^0-9a-f]/i', '', $_POST['fmp_set_screencolor']), 0, 6),
		'fmp_set_shuffle'		=> empty($_POST['fmp_set_shuffle']) ? 0 : 1,
		'fmp_set_showcontrols'		=> empty($_POST['fmp_set_showcontrols']) ? 0 : 1,
		'fmp_set_transition'			=> clean_slweg($_POST['fmp_set_transition']),
		'fmp_set_flashversion'		=> preg_replace('/[^0-9.r]/i', '', $_POST['fmp_set_flashversion']),
		'fmp_set_overstretch'		=> clean_slweg($_POST['fmp_set_overstretch']),
		'fmp_add_id'				=> intval($_POST['fmp_add_id']),
    'fmp_set_repeat'		=> empty($_POST  ['fmp_set_repeat']) ? 0 : 1,
    'fmp_set_rotatetime' 		=> intval($_POST ['fmp_set_rotatetime']),
    'cplayer_pos' 		=> intval($_POST['cplayer_pos']),
    'fmp_script'		=> clean_slweg($_POST['fmp_script']),
    'fmp_audio_file'		=> clean_slweg($_POST['fmp_audio_file']),
    'fmp_audio_id'		=> intval($_POST['fmp_audio_id']),
    'fmp_set_useaudio'		=> empty($_POST  ['fmp_set_useaudio']) ? 0 : 1,
    'fmp_set_kenburns'		=> empty($_POST  ['fmp_set_kenburns']) ? 0 : 1,
    'fmp_set_hidetitles'		=> empty($_POST  ['fmp_set_hidetitles']) ? 0 : 1,
    'fmp_set_volume' 		=> intval($_POST ['fmp_set_volume']),
    'fmp_set_externalcomm'		=> empty($_POST  ['fmp_set_externalcomm']) ? 0 : 1,
    'fmp_set_linktarget'		=> clean_slweg($_POST['fmp_set_linktarget']),
    'fmp_set_fullscreen'		=> empty($_POST  ['fmp_set_fullscreen']) ? 0 : 1,
    'xml_title'		=> clean_slweg($_POST['xml_title']),
    'fmp_set_wmode'		=> clean_slweg($_POST['fmp_set_wmode'])
				 );

foreach($_POST['fmp_order'] as $key => $value) {

   if (empty($_POST['fmp_remove_id']) || $_POST['fmp_remove_id']!=$value) {

  	$field_counter = $key;
    $content['imagerotator']['fields'][$field_counter]['fmp_int_ext']				= empty($_POST['fmp_int_ext'][$value]) ? 0 : 1;
		$content['imagerotator']['fields'][$field_counter]['fmp_internal_id']			= intval($_POST['fmp_internal_id'][$value]);
		$content['imagerotator']['fields'][$field_counter]['fmp_internal_dirid']			= intval($_POST['fmp_internal_dirid'][$value]);
    $content['imagerotator']['fields'][$field_counter]['fmp_internal_name']			= clean_slweg($_POST['fmp_internal_name'][$value]);
		$content['imagerotator']['fields'][$field_counter]['fmp_external_file']			= clean_slweg($_POST['fmp_external_file'][$value]);
		$content['imagerotator']['fields'][$field_counter]['fmp_title']				= clean_slweg($_POST['fmp_title'][$value]);
    if ($_POST['fmp_link'][$value]) {
    $parse_url = parse_url(clean_slweg($_POST['fmp_link'][$value]));
    $content['imagerotator']['fields'][$field_counter]['fmp_link'] = (isset($parse_url['scheme'])) ? clean_slweg($_POST['fmp_link'][$value])  : 'http://'.clean_slweg($_POST['fmp_link'][$value]);
    } else {
      $content['imagerotator']['fields'][$field_counter]['fmp_link'] = '';
    }
		//$content['imagerotator']['fields'][$field_counter]['fmp_link']					= clean_slweg($_POST['fmp_link'][$value]);

  }
}

// sort form fields
ksort($content['imagerotator']['fields'], SORT_NUMERIC);

//xml writing
/*
Note: in the playlists we need only xml wellformed formatted text
the char replacement for flashvars do not come in the playlist!
*/
$playlist_tmp = "";
if(count($content['imagerotator']['fields'])>0) { //should be always true

    $i=0;
    foreach($content['imagerotator']['fields'] as $row){

      // file
	    if($row['fmp_int_ext']) {
        // external files
        
        //if entry file is empty then we don't want it in the playlist
        if ($row['fmp_external_file']) {

          //start the output for the entry
          $playlist_tmp .= "<track>\n";
          
          //retrieve title
          if($row['fmp_title'] && $content['imagerotator']['fmp_set_hidetitles']==0) {
            $playlist_tmp .= "<title>".htmlspecialchars($row['fmp_title'], ENT_QUOTES)."</title>\n";
          }

          // retrieve link
          //link target global since v2, the following just to keep it kompatible with v1
          if($row['fmp_link']) {
		        $link_tmp = explode(' ', $row['fmp_link']);
            $playlist_tmp .= "<info>".htmlspecialchars($link_tmp[0])."</info>\n"; //we need xml standard
          }

          //files
          $playlist_tmp .= "<location>".$row['fmp_external_file']."</location>\n";

          $playlist_tmp .= "</track>\n";
        }
 
      } else if($row['fmp_internal_dirid']) {
      // internal all from folder
      
		    $sql = 'SELECT * FROM '.DB_PREPEND.'phpwcms_file WHERE f_aktiv=1 AND f_public=1 AND f_trash=0 AND f_pid='.$row['fmp_internal_dirid'] .' AND f_ext IN ("jpg", "png", "gif") ORDER BY f_name';
        $result = mysql_query($sql);
	      
        while($folderfile = mysql_fetch_array($result)) {

          //write entry in xml
          $playlist_tmp .= "<track>\n";

          // retrieve title
          if($row['fmp_title'] && $content['imagerotator']['fmp_set_hidetitles']==0) {
            $playlist_tmp .= "<title>".htmlspecialchars($row['fmp_title'], ENT_QUOTES)."</title>\n";
          }

          // retrieve link
          if($row['fmp_link']) {
            $temp_link = explode(' ', $row['fmp_link']);
            $playlist_tmp .= "<info>".htmlspecialchars($temp_link[0], ENT_QUOTES)."</info>\n";
          }

          // file
          if(isset($folderfile['f_id']) ) {
            $content['imagerotator']['xml'][$i]['file']		= $folderfile;
            $thumb_info = @getimagesize(PHPWCMS_URL.PHPWCMS_FILES.$folderfile["f_hash"] . '.' . $folderfile["f_ext"]);
          
            $zoominfo	 = get_cached_image(
              array(	"target_ext"	=>	$folderfile["f_ext"],
								"image_name"	=>	$folderfile["f_hash"] . '.' . $folderfile["f_ext"],
								"max_width"		=>	$thumb_info[0],
								"max_height"	=>	$thumb_info[1],
								"thumb_name"	=>	md5(	$folderfile["f_hash"].$thumb_info[0].
															$thumb_info[1].$GLOBALS['phpwcms']["sharpen_level"])
              )
            ); 		
            $content['imagerotator']['xml'][$i]['file']  = PHPWCMS_URL.PHPWCMS_IMAGES.$zoominfo[0];	//this should be xml valid already
		      } else {
            $content['imagerotator']['xml'][$i]['file'] = '';
          }
          $playlist_tmp .= "<location>".$content['imagerotator']['xml'][$i]['file']."</location>\n";

          $playlist_tmp .= "</track>\n";
	    
          $i++;
        } //end while

      } else if($row['fmp_internal_id']) {
        // internal 1 image
        
		    $sql = 'SELECT * FROM '.DB_PREPEND.'phpwcms_file WHERE f_aktiv=1 AND f_public=1 AND f_id='.$row['fmp_internal_id'];
		    $temp_internal_file = _dbQuery($sql);

        //write entry in xml
        $playlist_tmp .= "<track>\n";

        if($row['fmp_title'] && $content['imagerotator']['fmp_set_hidetitles']==0) {
          $playlist_tmp .= "<title>".htmlspecialchars($row['fmp_title'], ENT_QUOTES)."</title>\n";
        }

      	// retrieve link
        if($row['fmp_link']) {
		      $temp_link = explode(' ', $row['fmp_link']);
          $playlist_tmp .= "<info>".htmlspecialchars($temp_link[0], ENT_QUOTES)."</info>\n";
        }

        if(isset($temp_internal_file[0])) {
          $thumb_info = @getimagesize(PHPWCMS_URL.PHPWCMS_FILES.$temp_internal_file[0]["f_hash"] . '.' . $temp_internal_file[0]["f_ext"]);
          $zoominfo	 = get_cached_image(
						array(	"target_ext"	=>	$temp_internal_file[0]["f_ext"],
								"image_name"	=>	$temp_internal_file[0]["f_hash"] . '.' . $temp_internal_file[0]["f_ext"],
								"max_width"		=>	$thumb_info[0],
								"max_height"	=>	$thumb_info[1],
								"thumb_name"	=>	md5(	$temp_internal_file[0]["f_hash"].$thumb_info[0].
															$thumb_info[1].$GLOBALS['phpwcms']["sharpen_level"])
						)
        	); 			
          $content['imagerotator']['xml'][$i]['file']  = PHPWCMS_URL.PHPWCMS_IMAGES.$zoominfo[0];
		    } else {
          $content['imagerotator']['xml'][$i]['file'] = '';
		    }
        $playlist_tmp .= "<location>".$content['imagerotator']['xml'][$i]['file']."</location>\n";
      
        $playlist_tmp .= "</track>\n"; 
      
      } else {
      //we have no file
      continue;
      }

    $i++;
    } //end foreach
    
    $title_tmp = ($content['imagerotator']['xml_title']) ? htmlspecialchars($content['imagerotator']['xml_title'], ENT_QUOTES) : 'PHP Generated Playlist';
    $info_tmp = (PHPWCMS_URL) ? PHPWCMS_URL : 'http://www.phpwcms.de';
    //write xml
    $playlist = "<";
    $playlist .= "?xml version='1.0' encoding='UTF-8'?";
    $playlist .= ">\n";
    $playlist .= "<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n";
    $playlist .= "<title>".$title_tmp."</title>\n";
    $playlist .= "<info>".$info_tmp."</info>\n";
    $playlist .= "<trackList>\n";
    $playlist .= $playlist_tmp;
    $playlist .= "</trackList>\n";
    $playlist .= "</playlist>\n";
    
    //check for the next cp id when creating a new cp
    //neccessary because at this point phpwcms hasn't set the cp id yet
    $xml_cp_id = $_POST["cid"];
    if($_POST["cid"]==0) {
        $result7 = mysql_query("SHOW TABLE STATUS LIKE '".DB_PREPEND."phpwcms_articlecontent'") or die("Error: ".mysql_error());
        $rows7 = mysql_fetch_assoc($result7);
        $xml_cp_id = $rows7['Auto_increment'];
    }
    
    //write xml to file - this must have CHMOD 777
    write_textfile('content/tmp/playlist.rotator_'.$_POST["caid"].'_'.$xml_cp_id.'.xml', $playlist);

} //end if

?>